package site.wuao.simplerxjava.operator;


import android.util.Log;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * 操作符 - 条件
 *
 * @author wuao
 * @date 2017.12.08
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */
public class ConditionOperator {
    /** 标注 */
    private static final String TAG = "ConditionOperator";

    /**
     * amb
     *
     * @note 只发射首先发射数据或者通知的那个Observable的所有数据
     */
    public void amb() {
        Observable
                .amb(Observable.just(1, 2, 3).delay(2, TimeUnit.SECONDS), Observable.just(4, 5, 6))
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 4
        // call: 5
        // call: 6
    }

    /**
     * defaultIfEmpty
     *
     * @note 如果原始Observable没有发射数据，就会发射一个默认数据
     */
    public void defaultIfEmpty() {
        Observable
                .create(new Observable.OnSubscribe<Integer>() {
                    @Override
                    public void call(Subscriber<? super Integer> subscriber) {
                        subscriber.onCompleted();
                    }
                })
                .defaultIfEmpty(3)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 3
    }

    /**
     * takeUntil
     *
     * @note 直到true就停止
     */
    public void takeUntil() {
        Observable
                .just(1, 2, 3, 4, 5)
                .takeUntil(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer i) {
                        return i < 3;
                    }
                })
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer i) {
                        Log.i(TAG, "call: " + i);
                    }
                });

        // call: 1
    }

    /**
     * takeWhile
     *
     * @note 直到false就停止
     */
    public void takeWhile() {
        Observable
                .just(1,2,3,4,5)
                .takeWhile(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        return integer < 3;
                    }
                })
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 1
        // call: 2
    }

    /**
     * skipUntil
     *
     * @note SkipUntil订阅原始的Observable，但是忽略它的发射物，直到第二个Observable发射了一项数据那一刻，它开始发射原始Observable。
     */
    public void skipUntil() {
        Observable
                .interval(1,TimeUnit.SECONDS)
                .take(6)
                .skipUntil(Observable.just(10).delay(3,TimeUnit.SECONDS))
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        Log.i(TAG, "call: " + aLong);
                    }
                });

        // call: 2
        // call: 3
        // call: 4
        // call: 5
    }

    /**
     * skipWhile
     *
     * @note 1.舍弃原Observable发射的数据，直到发射的数据>=3，才继续发射
     * 2.SkipWhile订阅原始的Observable，但是忽略它的发射物，直到你指定的某个条件变为false的那一刻，它开始发射原始Observable。
     */
    public void skipWhile() {
        Observable
                .interval(1,TimeUnit.SECONDS)
                .take(6)
                .skipWhile(new Func1<Long, Boolean>() {
                    @Override
                    public Boolean call(Long aLong) {
                        Log.i(TAG, "call_Observable: " + aLong);
                        return aLong<3;
                    }
                })
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        Log.i(TAG, "call_Observer: " + aLong);
                    }
                });

        // call_Observable: 0
        // call_Observable: 1
        // call_Observable: 2
        // call_Observable: 3
        // call_Observer: 3
        // call_Observer: 4
        // call_Observer: 5
    }
}
